﻿using System;
using System.Collections.Generic;
using System.Linq;
using ETL.Business.Handlers.ProjectActualResourceHandler;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectTaskHandler
{
    public class GetProjectTasks
    {
        private readonly ProjectTaskRepository _projectTaskRepository;

        public GetProjectTasks(ProjectTaskRepository projectTaskRepository)
        {
            _projectTaskRepository = projectTaskRepository;
        }
        public GetProjectTasks()
        {
            _projectTaskRepository = new ProjectTaskRepository();
        }
        public virtual IEnumerable<ProjectTaskModel> Execute(decimal projectId, string userId)
        {
            IEnumerable<ProjectTaskModel> projectTasksModels = null;

            try
            {
                // try to get from db
                IEnumerable<TASK> projectTasksData = _projectTaskRepository.GetTasksOfProject(projectId, userId);

                // parse to business model
                if(projectTasksData != null && projectTasksData.Count() > 0)
                {
                    projectTasksModels = from prjTask in projectTasksData
                                            select new ProjectTaskModel(prjTask);
                    // Update percent complete
                    if(projectTasksModels != null && projectTasksModels.Count() > 0)
                    {
                        foreach (var tasks in projectTasksModels)
                        {
                            if(tasks.TaskId > 0)
                            {
                                ProjectActualResourceUsageModel lastestActualUsage =
                                        (new GetLastActualResourceUsage()).Execute(userId, projectId, tasks.TaskId);
                                if (lastestActualUsage.ActualResourceUsage != null)
                                {
                                    tasks.PercentComplete = lastestActualUsage.PercentComplete;
                                }
                            }


                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }



            return projectTasksModels;

        }

        public virtual IEnumerable<ProjectTaskModel> Execute(decimal projectId)
        {
            IEnumerable<ProjectTaskModel> projectTasksModels = null;

            try
            {
                // try to get from db
                IEnumerable<TASK> projectTasksData = _projectTaskRepository.GetTasksOfProject(projectId);

                // parse to business model
                if (projectTasksData != null && projectTasksData.Count() > 0)
                {
                    projectTasksModels = from prjTask in projectTasksData
                                         select new ProjectTaskModel(prjTask);
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }
            
            return projectTasksModels;

        }
    }
}
